// packageA/pages/refound/detail.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow: false,
    img_list:[],
    index:0,
    shipping_list:[],
    express_name:[],
    ret_id:[]
  },

  // 右侧导航
  toggle:function(){
    let that = this;
    that.setData({
      isShow:!that.data.isShow
    })
  },
  submit:function(e){
    let that = this;  
    let express_sn = e.detail.value.express_sn;
    let shipping_list = that.data.shipping_list;
    that.setData({
      express_name:shipping_list[0].shipping_id
    })

    if (typeof express_sn === "undefined" || express_sn == null || express_sn.trim() == "") {
      wx.showToast({
        title: '快递单号有误，请重新填写',
        icon: 'none'
      })
      return false;
    }

    let t = wx.getStorageSync("token");
    let express_name = that.data.express_name;
    let ret_id = that.data.ret_id;
    wx.request({
      url: app.apiUrl("refound/editexpress"),
      data: {
        ret_id: ret_id,
        express_name:express_name,
        express_sn:express_sn
      },
      header: {
        "Content-Type": "application/json",
        "X-ECTouch-Authorization": t
      },
      method: "POST",
      success: function (res) {
        console.log('editexpress',res.data)
        if (res.data.error == 0) {
          wx.showToast({
            title: '快递信息提交成功！',
            icon: 'success'
          })
          that.detail(ret_id);
        }else if (res.data.error == 1) {
          wx.showToast({
            title: '快递信息提交失败！'
          })
        }
      }
    });
  },
  bindPickerChange: function (e) {
    let that = this;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let shipping_list = that.data.shipping_list;
    console.log(shipping_list[e.detail.value].shipping_id)
    that.setData({
      index: e.detail.value,
      express_name:shipping_list[e.detail.value].shipping_id
    })
  },
  //预览图片
  PreviewImg: function (e) {
    let index = e.target.dataset.index;
    let that = this;
    // console.log(that.data.img_list[index]);
    // console.log(that.data.img_list);
    wx.previewImage({
      current: that.data.img_list[index],
      urls: that.data.img_list,
    })
  },
  //确认收货
  confirm_order: function(e) {
    var o = this;
    var t = wx.getStorageSync("token");
    wx.showModal({
        title: "提示",
        content: "确认收到商品？",
        success: function(i) {
            i.confirm && wx.request({
                url: a.apiUrl("user/order/confirm "),
                data: {
                    id: e.currentTarget.dataset.id
                },
                header: {
                    "Content-Type": "application/json",
                    "X-ECTouch-Authorization": t
                },
                method: "POST",
                success: function(t) {
                    0 == t.data.code ? (wx.showToast({
                        title: t.data.msg,
                        duration: 2e3
                    }), o.orderStatus(o, o.data.current), wx.redirectTo({
                        url: "../../../pages/user/index"
                    })) : wx.showToast({
                        title: t.data.msg,
                        image:"../../../images/failure.png",
                        duration: 2e3
                    });
                }
            });
        }
    });
},

  //加载详情
  detail: function (ret_id) {
    let that = this;
    let t = wx.getStorageSync("token");
    wx.request({
      url: app.apiUrl("refound/detail"),
      data: {
        ret_id: ret_id
      },
      header: {
        "Content-Type": "application/json",
        "X-ECTouch-Authorization": t
      },
      method: "POST",
      success: function (res) {
        // console.log('detail',res.data)
        let img_all = res.data.img_all;
        let img_list = [];
        for (let i = 0; i < img_all.length; i++) {
          img_list.push(app.globalData.url + img_all[i].img_file)
        }
        console.log(img_list)
        that.setData({
          detail:res.data,
          img_list:img_list,
          shipping_list:res.data.shipping_list
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    // console.log(options)
    that.detail(options.ret_id);
    that.setData({
      ret_id:options.ret_id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})